package inner.stream;

import day4.BankAcc;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author oldliu
 * @since 1.0
 */
public class BankAccTest {
    public static void main(String[] args) {
        //Runnable haha = () -> System.out.println("haha");
        List<BankAcc> list = new ArrayList<>();
        list.add(new BankAcc("hf--5", "小蔡", "3111", "hefei", 50333.0F));
        list.add(new BankAcc("hf--7", "陈毅", "3111", "hefei", 74894.0F));
        list.add(new BankAcc("hf--1", "老吴", "123", "shanghai", 1000000.0F));
        list.add(new BankAcc("hf--2", "小刘", "1243", "shanghai", 2030.0F));
        list.add(new BankAcc("hf--3", "小李", "1523", "nanjing", 60000.0F));
        list.add(new BankAcc("hf--4", "老张", "623", "shanghai", 1000.0F));
        //获得账户钱少于10000的人,输出
//        list.forEach(o->{
////            System.out.println(o);
////        });

        list.forEach(System.out::println);

//        List<BankAcc> collect = list.stream().filter((o) -> {
//            return o.getMoney() < 10000;
//        }).collect(Collectors.toList());
//
//        list.stream().filter((o)->{return o.getMoney()<10000;}).forEach(System.out::println);
////        for (BankAcc b : list) {
//            if (b.getMoney() < 10000)
//                System.out.println(b);
//        }
        //1:求最大
        //2：求平均
        //3：求总钱数
        //4：统计各个城市的账户信息， Map<城市，List<Bank>)
        //4：统计各个城市的账户人数,Map<城市，int)

        BankAcc max = list.stream().max((o1, o2) -> {
            return (int) (o1.getMoney() - o2.getMoney());
        }).get();
        System.out.println("钱最多的人:" + max);
        Double avg = list.stream().collect(Collectors.averagingDouble((o) -> {
            return o.getMoney();
        }));
        System.out.println("账户平均值:" + avg);
        Double total = list.stream().collect(Collectors.summingDouble((o) -> {
            return o.getMoney();
        }));
        System.out.println("总金额:" + total);
        //nanj,beijing,hefei
        //Map<String,List<BankAcc>
        Map<String, List<BankAcc>> cities = list.stream().collect(Collectors.groupingBy(BankAcc::getCity, Collectors.toList()));
        System.out.println("每个城市的账号信息：");
        cities.forEach((k, v) -> {
            System.out.println(k + "::::" + v);
        });
        //
        Map<String, Optional<BankAcc>> collect = list.stream().collect(Collectors.groupingBy(BankAcc::getCity, Collectors.maxBy((o, o1) -> {
            return (int) (o.getMoney() - o1.getMoney());
        })));
        System.out.println("每个城市钱最多的人");
        System.out.println(collect);
        //各个城市的钱平均值
        Map<String, Long> c2 = list.stream().collect(Collectors.groupingBy(BankAcc::getCity, Collectors.counting()));
        System.out.println("各个城市的人数：");
        System.out.println(c2);
        System.out.println(">>>>>>>>>>>>>>");
        List<BankAcc> l3 = list.stream().filter((o) -> {
            return o.getMoney() > 10000;
        }).collect(Collectors.toList());
        System.out.println("超过10000的人："+l3);
        List<BankAcc> l4 = list.stream().filter((o) -> {
            return o.getName().contains("张");
        }).collect(Collectors.toList());
        System.out.println("dai张的人："+l4);

    }


}
